home *** CD-ROM | disk | FTP | other *** search
- """Wrapper script for py_compile which reformats IndentationError and
- TabError into a format more palatable to BBEdit and TextWrangler's Python
- error parsers."""
-
- import py_compile
- import sys
-
- __revision__ = "$Revision: #1 $"
- __all__ = ['main']
-
- try:
- True, False
- except NameError:
- # Maintain compatibility with Python 2.2
- True, False = 1, 0
-
- def print_reformatted_error(exc_type_name, exc_value):
- """Print out IndentationError and TabError in the same format as
- SyntaxError"""
- msg = exc_value[0]
- file = exc_value[1][0]
- line = exc_value[1][1]
- col = exc_value[1][2]
- ctx = exc_value[1][3]
- if ctx.endswith('\r') or ctx.endswith('\n'):
- ctx = ctx[:-1]
- print >> sys.stderr, ''' File "%s", line %d''' % (file, line)
- print >> sys.stderr, ctx
- print >> sys.stderr, ' ' * col + '^'
- print >> sys.stderr, exc_type_name + ": " + msg
-
- def main():
- """Check syntax on the file passed in sys.argv[1]"""
- source_file = sys.argv[1]
- try:
- py_compile.compile(source_file, doraise=True)
- except py_compile.PyCompileError, ex:
- (c, v, t) = sys.exc_info()
- exc_names = [IndentationError.__name__, TabError.__name__]
- if v.exc_type_name in exc_names:
- print_reformatted_error(v.exc_type_name, v.exc_value)
- else:
- print >> sys.stderr, v,
- sys.exit(1)
-
- if __name__ == "__main__":
- main()
-